//Learned：
//1. 两个结构体要提供各自的，而不能“共用”
#include <iostream>
#include <string>
#include <jsoncpp/json/json.h>
using std::string;
using std::to_string;

#define MySelf 1

const char BlankSapceSep = ' ';
const char ProtocolSep = '\n';

// len\n1 + 2\n
bool Encode(const string& contents, string* package)
{
    *package = to_string(contents.size()) + ProtocolSep + contents + ProtocolSep;
    return true;
}

bool Decode(string& package, string* contents)
{
    size_t pos = package.find(ProtocolSep);
    if(pos == string::npos) return false;
    //获取报头信息
    string temp = package.substr(0,pos);
    int con_len = stoi(temp);
    
    //保证收到的是按协议制定的一条完整的报文
    int total_len = temp.size() + con_len + 2;
    if(package.size() < total_len)  return false;

    *contents = package.substr(pos+1,con_len);

    //可能收到多条完整报文，故处理完之后要将其删除
    package.erase(0,total_len);
    return true;
}


class Request
{
public:
    Request()
    {

    }

    Request(int x, int y, char op)
    :_x(x), _y(y), _op(op)
    {

    }

    //把结构化数据变为字符串    x=1 y=2 op='+' --> 1 + 2
    bool Serialization(string* contents)
    {
#ifdef MySelf
        *(contents) = to_string(_x) + BlankSapceSep + _op + BlankSapceSep + to_string(_y);
        return true;

#else
        Json::Value root;
        root["x"] = _x;
        root["y"] = _y;
        root["op"] = _op;
        Json::StyledWriter sw;
        *contents = sw.write(root);
#endif
    }

    //把字符串变为结构化数据 1 + 2 --> x=1 y=2 op='+'
    bool Deserialization(string* contents)
    {
#ifdef MySelf
        string temp = *contents;

        size_t left = temp.find(BlankSapceSep);
        if(left == string::npos)    return false;
        _x = stoi(temp.substr(0, left));

        size_t right = temp.rfind(BlankSapceSep);
        if(right == string::npos)   return false;
        _y = stoi(temp.substr(right+1));

        if(left == right)   return false;
        _op = temp[left+1];
        return true;
#else
        Json::Reader r;
        Json::Value v;
        string temp = *contents;
        r.parse(temp,v);

        _x = v["x"].asInt();
        _y = v["y"].asInt();
        _op = v["op"].asInt();
#endif
    }

public:
    int _x;
    int _y;
    char _op;
};

class Response
{
public:
    Response(){}
    //把结构化数据变为字符串    result=3 checkcode=0 --> 3 0
    bool Serialization(string* contents)
    {   
#ifdef MySelf
        *contents = to_string(_result) + BlankSapceSep +to_string(_checkcode);
        return true;
#else
        Json::Value root;
        root["result"] = _result;
        root["checkcode"] = _checkcode;
        Json::StyledWriter sw;
        *contents = sw.write(root);
#endif
    }

    //把字符串变为结构化数据    3 0 --> result=3 checkcode=0
    bool Deserialization(string* contents)
    {
#ifdef MySelf
        string temp = *contents;
        size_t pos = temp.find(BlankSapceSep);
        if(pos == string::npos)     return false;
        _result = stoi(temp.substr(0,pos));
        _checkcode = stoi(temp.substr(pos+1));
        return true;
#else
        Json::Reader r;
        Json::Value v;
        string temp = *contents;
        r.parse(temp,v);

        _result = v["result"].asInt();
        _checkcode = v["checkcode"].asInt();
#endif
    }
public:
    int _result = 0;
    int _checkcode = 0;
};